package com.fuhao.ack.tx;

import com.fuhao.utils.RabbitUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * 消息确认机制之事务模式
 *      此模式非常耗时，吞吐量低
 * @author zhang
 */
public class Product {
    public static void main(String[] args) throws IOException, TimeoutException {
        Connection connection = RabbitUtils.getConnection();
        Channel channel = connection.createChannel();
        channel.queueDeclare("ack_tx_queue",true,false,false,null);
        try{
            //选择事务机制
            channel.txSelect();
            String msg = "ack tx queue";
            channel.basicPublish("","ack_tx_queue",null,msg.getBytes());
            int i = 1/0;
            //提交事务
            channel.txCommit();
        }catch (Exception e){
            //回滚事务
            channel.txRollback();
            e.printStackTrace();
        }
        channel.close();
        connection.close();
    }
}
